//const version = '1.0.0'
const version = '1.0.11'
const client = 'web'
let userToken = ''
export default {
	async post(url,params,callback){
		// 报错不返回
		if (url.split('//')[1] == 'wy.canghan.top/api/login') {
		  params.latitude = uni.getStorageSync('Location').latitude || 0
		  params.longitude = uni.getStorageSync('Location').longitude || 0
		}
		assignParam(params || {})
		const data = Object.assign(
			{},
			{
				url,
				method: 'POST',
				header: {
					'content-type': 'application/x-www-form-urlencoded',
					'token': uni.getStorageSync('userToken'),
					// 'imei': params.imei,
					// 'client': params.client,
					'version': version,
					'imei': uni.getStorageSync('imei'),
					'client': client
				},
				data: params,
				success: res=>{
					if(res.data.code===0){
						callback(res.data)
					}else if(res.data.code===1){
						uni.hideLoading()
						uni.showModal({
							title: '提示',
							content: res.data.message,
							showCancel:false
						})
					} else if(res.data.status === 500 && (res.data.message==="token验证失败" || res.data.message==="token不合法")){
						uni.showModal({
							title: '提示',
							content: '请重新登录',
							showCancel:false,
							success:(res)=> {
								if (res.confirm) {
									// 去登录
									let pages = getCurrentPages()    //获取加载的页面
									let currentPage = pages[pages.length-1]    //获取当前页面的对象
									let url = currentPage.route    //当前页面url
									if (url != 'pages/user-login/user-login') {
										uni.navigateTo({
											url:'/pages/user-login/user-login'
										})
									} else{
										return false
									}
								}
							}
						})
						console.log(url);
						console.log(res);
						console.log('登录状态失效')
					} else {
						if (url.split('api')[1] == '/door/open') {
							uni.hideLoading()
							uni.showModal({
								title: '提示',
								content: res.data.message,
								showCancel:false
							})
							callback(res)
						}
						if (url.split('api')[1] == '/team/voicecontrol') {
							uni.hideLoading()
							res.data.err = true
							return callback(res.data)
						}
						uni.hideLoading()
						if(res.data.message == '无权限') {
							uni.showModal({
								title: '提示',
								content: res.data.message,
								showCancel:false,
								success(res) {
									if(res.confirm) {
										uni.navigateBack({
											delta:1
										})
									}
								}
							})
							
						}else{
							uni.showModal({
								title: '提示',
								content: res.data.message,
								showCancel:false
							})
						}
					}
				}
			}
		)
		await uni.request(data)
	},
	async postback(url,params,callback){
		// 报错返回上级页面
		if (url.split('//')[1] == 'wy.canghan.top/api/login') {
		  params.latitude = uni.getStorageSync('Location').latitude || 0
		  params.longitude = uni.getStorageSync('Location').longitude || 0
		}
		assignParam(params || {})
		const data = Object.assign(
			{},
			{
				url,
				method: 'POST',
				header: {
					'content-type': 'application/x-www-form-urlencoded',
					'token': uni.getStorageSync('userToken'),
					'version': version,
					'imei': uni.getStorageSync('imei'),
					'client': client
				},
				data: params,
				success: res=>{
					if(res.data.code===0){
						callback(res.data)
					}else if(res.data.code===1){
						uni.hideLoading()
						uni.showModal({
							title: '提示',
							content: res.data.message,
							showCancel:false
						})
					} else if(res.data.status === 500 && (res.data.message==="token验证失败" || res.data.message==="token不合法")){
						uni.showModal({
							title: '提示',
							content: '请重新登录',
							showCancel:false,
							success:(res)=> {
								if (res.confirm) {
									// 去登录
									let pages = getCurrentPages()    //获取加载的页面
									let currentPage = pages[pages.length-1]    //获取当前页面的对象
									let url = currentPage.route    //当前页面url
									if (url != 'pages/user-login/user-login') {
										uni.navigateTo({
											url:'/pages/user-login/user-login'
										})
									} else{
										return false
									}
								}
							}
						})
						console.log(url);
						console.log(res);
						console.log('登录状态失效')
					} else {
						uni.hideLoading()
						uni.showModal({
							title: '提示',
							content: res.data.message,
							showCancel:false,
							success:(res)=> {
								if (res.confirm) {
									uni.navigateBack({})
								}
							}
						})
					}
				}
			}
		)
		await uni.request(data)
	},
	async get(url,params){
		if (url.split('//')[1] == 'wy.canghan.top/api/login') {
		  params.latitude = uni.getStorageSync('Location').latitude || 0
		  params.longitude = uni.getStorageSync('Location').longitude || 0
		}
		assignParam(params || {})
		return new Promise (function(resolve, reject){
			uni.request(
				Object.assign(
					{},
					{
						url,
						method: 'GET',
						header: {
							'content-type': 'application/json',
							'token': uni.getStorageSync('userToken'),
							'version': version,
							'imei': uni.getStorageSync('imei'),
							'client': client
						},
						data: params,
						success: res =>{
							if(res.data.code===0){
								resolve(res.data)
							} else if(res.data.status == 500 && (res.data.message==="token验证失败" || res.data.message==="token不合法")){
								uni.showModal({
									title: '提示',
									content: '请重新登录',
									showCancel:false,
									success:(res)=> {
										if (res.confirm) {
											// 去登录
											let pages = getCurrentPages()    //获取加载的页面
											let currentPage = pages[pages.length-1]    //获取当前页面的对象
											let url = currentPage.route    //当前页面url
											if (url != 'pages/user-login/user-login') {
												uni.navigateTo({
													url:'/pages/user-login/user-login'
												})
											} else{
												return false
											}
										}
									}
								})
							} else if (res.data.status == 500) {	
								console.log(77,url.split('api')[1])
								uni.hideLoading()
								uni.showModal({
									title: '提示',
									content: res.data.message,
									showCancel:false
								})
							}else {
								reject(res.data.message)
							}
						}
					}
				)
			)
		})
	}
}
const createUUID = function () {
  function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
  }
  return (S4() + S4() + '-' + S4() + '-' + S4() + '-' + S4() + '-' + S4() + S4() + S4())
}
function assignParam(params) {
  let cookieImei = uni.getStorageSync('imei')
  if (!cookieImei) {
    var expiresDate = new Date()
    expiresDate.setTime(expiresDate.getTime() + (43200 * 60 * 1000))
    uni.setStorageSync('imei', createUUID())
    cookieImei = uni.getStorageSync('imei')
  }
  const imei = cookieImei
  params.version = version
  params.imei = imei
  params.client = client
  return false
}
